define(function () {    
    var ec=require('echarts');
    require('echarts/chart/pie'); 
    require('echarts/chart/funnel');

    var sourcePie = ec.init(document.getElementById('source_plat_pie'));
    var option_day = {
        tooltip : {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            orient : 'vertical',
            x : 'left',
            data:[]
        },
        toolbox: {
            show : true,
            feature : {
                mark : {show: true},
                dataView : {show: true, readOnly: false},
                magicType : {
                    show: true, 
                    type: ['pie', 'funnel']
                },
                restore : {show: true},
                saveAsImage : {show: true}
            }
        },
        calculable : false,
        series : [
            {
                name:'平台',
                type:'pie',
                selectedMode: 'single',
                radius : [0, 90],
                // for funnel
                x: '20%',
                width: '40%',
                funnelAlign: 'right',
                max: 100000000,
                itemStyle : {
                    normal : {
                        label : {
                            show : true,
                            position:'inner',
                            distance:0.7
                        },
                        labelLine : {
                            show : false
                        }
                    },
                    emphasis : {
                        label : {
                            show : true,
                            position : 'inner',
                           distance:0.7,
                            textStyle : {
                                fontSize : '13',
                                fontWeight : 'bold'
                            }
                        }
                    }
                },
                data:[]
            },
            {
                name:'来源',
                type:'pie',
                radius : [110, 140],
                
                // for funnel
                x: '60%',
                width: '35%',
                funnelAlign: 'left',
                max: 50000000,
                data:[]
            }
        ]
    };

    var initalData;//原始数据
    var ecConfig = require('echarts/config');
    sourcePie.on(ecConfig.EVENT.PIE_SELECTED, function (param){
        var selected = param.selected;
        var serie;
        var str = '当前选择： ';
        for (var idx in selected) {
            if(idx != 0)
                continue;
            serie = option_day.series[idx];
            for (var i = 0, len = serie.data.length; i < len; i++) {
                if (selected[idx][i]) {
                    var selected_src1 = serie.data[i].name; //选中的一级来源
                    var legend_source2 = initalData[selected_src1].source2NameList; //对应的二级来源
                    var data_source2 = initalData[selected_src1].source2DataList;
                    var uniqueSc2 = [];
                    $.each(legend_source2, function(i, n){
                       if($.inArray(n,initalData.source1NameList) == -1){
                            uniqueSc2.push(n);
                       }
                    });
                    option_day.legend.data = initalData.source1NameList.concat(uniqueSc2);
                    option_day.series[1].data = data_source2;
                    option_day.series[1].max = initalData[selected_src1].max;
                    // str += '【系列' + idx + '】' + serie.name + ' : ' + '【数据' + i + '】' +  serie.data[i].name+ ' ';
                    // alert(str);
                }
            }
        }
        sourcePie.setOption(option_day,true);
    });

    function resetOption(){
        $('#beginDate_plat').val(initalData.beginDate);
        // $('#endDate_plat').val(initalData.endDate);
        option_day.legend.data = initalData.source1NameList;
        option_day.series[0].data = initalData.source1DataList;
        option_day.series[1].data = [];
        option_day.series[0].max = initalData.max;
        var series_local = [];                              
        // sourcePie.clear();
        sourcePie.setOption(option_day,true);
        sourcePie.hideLoading();
    }


    /**
     * 根据指标显示日数据
     * @param index
     */
    function show(businessName){
        var beginDate = $('#beginDate_plat').val();
        var userType = $('#userType_platSrc').val();
        var indexType = $('#index_userType_platSrc').val();
        var cate2Name = $('#cate2_userType_platSrc').val();

        sourcePie.showLoading({
            text: '正在努力的读取数据中...'
        });
        $.ajax({
            url: '/dashboard/info/ajax/platSrc',
            type: 'post',
            async: true,
            data:{"businessName":businessName,"userType":userType,"indexType":indexType,"beginDate":beginDate},
            dataType: 'json',
            success: function(data, textStatus) {
                initalData=data;
                resetOption();
            },
            error : function() {
                sourcePie.hideLoading();
            }
        });
    }

    //时间选择事件
    $('#beginDate_plat').datepicker(datepicker_op);
    // $('#endDate_plat').datepicker(datepicker_op);
    
    // $('#beginDate_plat').datepicker().on('changeDate', function (e) {
    //     $('#endDate_plat').datepicker('setStartDate', e.date);
    // })

    // $('#endDate_plat').datepicker().on ('changeDate', function (e) {
    //     $('#beginDate_plat').datepicker('setEndDate', e.date);
    // });


    //绑定刷新
    $("#refresh_platSrc_btn").click(function(){     
        EC.show(businessName);
    });
        

    // var zrnumber = require('echarts/util/number');
    // var zrUtil = require('zrender/tool/util');

    var EC={
        echart:sourcePie,
        resize:function(){sourcePie.resize();},
        'show':show
    };
    return EC;
});